import miniApp, {
    analyticsBase,
    authorizeParams,
    gameRecorderStartParams,
    getUserInfoParams,
    loadParams,
    loginParams, ModalParams, SHARE_VIDEO_SORT_TYPE, ShareVideoRankDetail,
    toastParams
} from "../miniApp";
import gameConfig from "../../../configs/gameConfig";
import O1HD from "../../../gameEngine";
import {OnloadCB} from "../vivo/vivoApi";
import {dataField} from "../dataField";

export interface TtRewardedVideoAdParam {
    // 名称	数据类型	属性	默认值	描述
    adUnitId: string;		//required	广告位 id
}

export interface OppoVideoAd {
    // 通过 load 方法主动预加载广告内容。
    // 此外，在显示广告出现问题时也可以尝试主动 load 一次。
    // 该方法返回一个 Promise，如果广告已经自动拉取成功，调用该方法返回一个 resolved Promise；
    load: () => Promise<any>;
    // 广告创建后默认是隐藏的，可以通过该方法显示广告。 该方法返回一个 Promise 对象。当广告组件正常获取素材时，该 Promise 对象会是一个 resolved Promise。当广告组件发生错误时，会是一个 rejected Promise，参数与error事件监听器获得的参数相同。
    show: () => Promise<any>;
    //绑定 load 事件的监听器，用户在手动调用 load 方法后，广告组件会预先加载。 广告组件成功拉取广告素材时会触发load事件的监听器。
    //确保监听此事件时，开发者主动调用过 load 方法。
    onLoad: (listener: Function) => void;
    // 解除绑定 load 事件的监听器。
    offLoad: (listener: Function) => void;
    //绑定 error 事件的监听器。 广告组件拉取广告素材和其他情况下如果发生错误，会触发error事件的监听器。监听器会获得一个包含errCode和errMsg属性的对象参数。
    //     errCode 详情
    onError: (listener: (error: any) => void) => void;
    //    解除绑定 error 事件的监听器。
    offError: (listener: Function) => void;
    //    绑定 close 事件的监听器。 当用户点击了 Video 广告上的关闭按钮时，会触发close事件的监听器。监听器会获得一个包含isEnded属性的对象参数，表征用户是否完整观看了视频。
    onClose: (listener: (res: { isEnded: boolean }) => void) => void;
    //解除绑定 close 事件的监听器。
    offClose: (listener: Function) => void;

    //
    destroy();
}

const oppo: any = window['qg'];
export default class OppoApi extends miniApp {
    async commitRank(rankId: dataField.RANK_ID, score: number): Promise<boolean> {
        console.error('#48102640 未实现');
        return Promise.resolve(false);
    }

    async showRank(rankId: dataField.RANK_ID): Promise<boolean> {
        console.error('#51102648 未实现');
        return Promise.resolve(false);
    }

    navigateToMiniGame(appId: string, successCallback?: () => void, failCallback?: () => void): void {
        console.error('#54102651 未实现跳转');
    }

    vibrate(type): void {

    }

    addShortcut(): Promise<boolean> {
        // throw new Error("Method not implemented.");
        return null;
    }

    getShareVideoRank(sortType: SHARE_VIDEO_SORT_TYPE, successCb: (data: ShareVideoRankDetail[]) => void): void {
        // throw new Error("Method not implemented.");
    }

    drawUrlImage(url: string, overCb: (spf: cc.SpriteFrame) => void): void {
        // throw new Error("Method not implemented.");
    }

    showVideo(videoUrl: string): void {
        // throw new Error("Method not implemented.");
    }

    subscribeMessage(successCallback?: () => void, failCallback?: () => void, completeCallback?: () => void): void {
        // throw new Error("Method not implemented.");
    }

    showAddFavoriteGuide(successCallback?: () => void, failCallback?: () => void, completeCallback?: () => void): void {
        // throw new Error("Method not implemented.");
    }

    StartRecordSound(): void {
        // throw new Error("Method not implemented.");
    }

    StopRecordSound(successCallback?: () => void): void {
        // throw new Error("Method not implemented.");
    }

    PlayLastRecordSound(scale?: number, successCb?: () => void, errorCb?: () => void): void {
        // throw new Error("Method not implemented.");
    }

    operateGameLive(): void {
        // throw new Error("Method not implemented.");
    }

    protected m_appId: string = '30490355';
    protected m_secret: string = '5b004Cc3F9399878C51edf973F7af00C';
    protected m_adUnitId: string = '301459';
    protected m_PortalId: string = "301461";
    protected m_nAdIdList: string[] = ["305202", "305201", "305200"];

    protected m_BannerId: string = "304800";
    protected m_BannerIdList: string[] = ["304799", "304800", "304797"];

    //protected m_adUnitIdList: string[] = ['1flm74jfpgml93lhhb', '134he4gc7igj13du0e', 'pca9a4n61d9ak41ouq'];

    // protected m_InsertId: string = "";

    //录屏管理器
    private m_gameRecorderManager: any = null;
    //录屏文件保存完地址
    private m_gameRecordVideoUrl: string = null;
    //视频广告组件
    private m_videoAd: OppoVideoAd = null;

    getUserInfo(params: getUserInfoParams): void {
        // oppo.getUserInfo(params);
    }

    showToast(params: toastParams): void {
        oppo.showToast(params);
    }

    showLoading(params: loadParams): void {
        oppo.showLoading(params);
    }

    hideLoading(): void {
        oppo.hideLoading()
    }

    preCreateUnits(): void {
        this.CheckLocalToBlockCityList();
        // this.createRewardedVideovalidAppIdAd({ adUnitId: this.m_adUnitId });
        // this.m_gameRecorderManager = oppo.getGameRecorderManager();
    }

    private onShareAppMessageGetReward(successCallback: () => void,
                                       errorCallback: (error: any) => void,
                                       completeCallback: () => void, isNeedModal?: boolean) {
        //
        // let shareAppRewardFunction = () => {
        //     oppo.shareAppMessage({
        //         templateId: "2w1ji856pahdij2f29", // 替换成通过审核的分享ID
        //         query: "",
        //         success: () => {
        //             console.log("oppo 分享成功");
        //             successCallback();
        //             completeCallback && completeCallback();
        //         },
        //         fail: (e) => {
        //             console.log("oppo 分享失败");
        //             errorCallback(e);
        //             completeCallback && completeCallback();
        //         },

        //     })
        // }
        // //
        // if (isNeedModal) {
        //     oppo.showModal({
        //         title: '游戏奖励',
        //         content: '视频拉取失败,分享游戏也可领取视频奖励',
        //         confirmText: '分享',
        //         showCancel: true,
        //         success: (info: { errMsg: string, confirm: boolean, cancel: boolean }) => {
        //             console.log('oppo 分享success');
        //             if (info.confirm) {
        //                 shareAppRewardFunction();
        //             } else {
        //                 errorCallback(null);
        //                 completeCallback();
        //             }
        //         },
        //     })
        // } else {
        //     shareAppRewardFunction();
        // }
    }

    createMoreGameBtn()//创建 <更多游戏> 按钮
    {
        // const buttonWithText = oppo.createMoreGamesButton({
        //     type: "text",
        //     style: {
        //         left: 120,
        //         top: 400,
        //         width: 120,
        //         height: 100,
        //         backgroundColor: "#ffffff",
        //         borderColor: "#ffff00",
        //         borderWidth: 10,
        //         borderRadius: 10,
        //         textAlign: "left",
        //         fontSize: 13,
        //         lineHeight: 50,
        //         textColor: "#000000",
        //     },
        //     text: "text的button",
        //     actionType: "box",
        //     appLaunchOptions: [
        //         {
        //             appId: this.m_appId,
        //             query: oppo.getLaunchOptionsSync().query,
        //             extraData: {},
        //         },
        //         // {...}
        //     ],
        //     onNavigateToMiniGameBox(res) {
        //         console.log("跳转到小游戏盒子", res);
        //     },
        // });
    }

    showInterstitialAd(onAdClose: Function, onFailed: Function) {//展示插屏广告！！！！
        // if (oppo) {

        //     console.log("加抖音 插屏 广告");
        //     var interstitialAd = oppo.createInterstitialAd({
        //         adUnitId: this.m_InsertId,
        //     })
        //     interstitialAd.load()
        //     interstitialAd.onLoad(() => {
        //         console.warn('插屏广告 加载完成');

        //         interstitialAd.show().catch((err) => {
        //             console.warn('插屏广告 显示失败 ：' + JSON.stringify(err))
        //             interstitialAd.destroy();
        //             interstitialAd = null;
        //             if (onFailed) {
        //                 onFailed();
        //             }
        //         })

        //     })

        //     interstitialAd.onError((err) => {
        //         console.warn('插屏广告 加载失败' + JSON.stringify(err));
        //         interstitialAd.destroy();
        //         interstitialAd = null;
        //         if (onFailed) {
        //             onFailed();
        //         }
        //     })

        //     interstitialAd.onClose(() => {
        //         console.warn('插屏广告 关闭');
        //         if (interstitialAd != null) {
        //             interstitialAd.destroy();
        //             interstitialAd = null;
        //         }
        //         if (onAdClose) {
        //             onAdClose();
        //         }
        //     })
        // } else {
        //     onAdClose();
        // }
    }

    //原生广告
    private m_nativeAd = null;
    private nowNAdId: number = 0;
    private m_nAdlist: OnloadCB;
    //banner广告
    private mowBanner: number = 0;

    /** 加载原生广告 */
    showNativeAd(successCallback: (a?: any) => void,
                 errorCallback: (error: any) => void,
                 completeCallback?: () => void) { //原生广告
        console.log("----------原生广告判断前----------");
        if (oppo) {
            console.log("----------原生广告判断后----------");
            if (this.m_nativeAd) {
                this.m_nativeAd.destroy();
                this.m_nativeAd = null;
            }

            this.m_nativeAd = oppo.createNativeAd({posId: this.m_nAdIdList[this.nowNAdId]});

            this.m_nativeAd.load().then((res) => {
                console.log(res.adList[0].creativeType, "creativeType======")
                console.log("原生广告加载成功..." + res);
                console.log(JSON.stringify(res), "this.m_nativeAd=======");
                console.log("------------  res.adList : " + res.adList);
                // console.log("------------  res.adList.adId : " + res.adList.adId);  //错误使用方法
                console.log("------------  res.adList[0].adId : " + res.adList[0].adId);
                console.log("------------  res.adList[0].imgUrlList[0] : " + res.adList[0].imgUrlList[0]);
                console.log("------------  this.m_nAdlist.imgUrlList[0] : " + JSON.stringify(this.m_nAdlist.imgUrlList));

                this.nowNAdId++;
                if (this.nowNAdId == this.m_nAdIdList.length) {
                    this.nowNAdId = 0;
                }

                completeCallback && completeCallback();
            }).catch((res) => {
                console.log("原生广告加载失败..." + JSON.stringify(res));

                errorCallback(res);
                completeCallback && completeCallback();
            });

            this.m_nativeAd.onLoad((res) => {

                this.m_nAdlist = res.adList[0];
                this.m_nativeAd.reportAdShow({adId: this.m_nAdlist.adId});  //原生广告曝光上报

                successCallback(this.m_nAdlist);

                console.log(JSON.stringify(this.m_nativeAd), "this.m_nativeAd=======");
                console.log("------------  res.adList : " + res.adList);
                // console.log("------------  res.adList.adId : " + res.adList.adId);  //错误使用方法
                console.log("------------  res.adList[0].adId : " + res.adList[0].adId);
                console.log("------------  res.adList[0].imgUrlList[0] : " + res.adList[0].imgUrlList[0]);
                console.log("------------  this.m_nAdlist.imgUrlList[0] : " + JSON.stringify(this.m_nAdlist.imgUrlList));

            });
        }
    }

    /** 原生广告点击上报 */
    reportAdClickNativeAd() {
        if (!this.m_nativeAd) {
            return;
        }
        this.m_nativeAd.reportAdClick({adId: this.m_nAdlist.adId});
    }

    // protected m_Bannerad: any = null
    // private bannerSuccese: boolean = true

    // //互推盒子-横幅
    // private m_bannerAd: any = null;
    //互推盒子-九宫格
    private m_portalAd: any = null;

    protected m_Bannerad: any = null
    private bannerSuccese: boolean = true

    showBannerAD(node: cc.Node,
                 successCallback?: () => void,
                 errorCallback?: (error: any) => void,
                 onCloseCallBack?: () => void,
                 setBannerPosition?: ({screenWidth, screenHeight}) => cc.Vec2)//展示banner广告
    {

        // const { screenWidth, screenHeight } = oppo.getSystemInfoSync();
        const {screenWidth, screenHeight} = oppo.getSystemInfoSync();
        let pos: cc.Vec2 = setBannerPosition && setBannerPosition({screenWidth, screenHeight}) || cc.v2(0, 0);

        var targetBannerAdWidth = 200;
        if (this.m_Bannerad && !this.bannerSuccese) {
            this.m_Bannerad.destroy()
            this.m_Bannerad = null
        }

        if (this.m_Bannerad) {
            this.m_Bannerad.show()
                .then(() => {
                    successCallback && successCallback();
                    console.log("---------------------Banner广告直接显示成功");
                })
                .catch((err) => {
                    errorCallback && errorCallback(err);
                    console.log("---------------------Banner广告直接显示组件出现问题", JSON.stringify(err));
                });
            return
        }

        this.bannerSuccese = true
        if (this.m_Bannerad == null) {
            this.m_Bannerad = oppo.createBannerAd({
                adUnitId: this.m_BannerIdList[this.mowBanner],
                adIntervals: 30,
                style: {
                    // width: targetBannerAdWidth,
                    // top: screenHeight - (targetBannerAdWidth / 16) * 9, // 根据系统约定尺寸计算出广告高度
                    left: pos.x,
                    top: pos.y,
                }
            });//创建
        }

        this.m_Bannerad.onResize(size => {
            console.log(size.width, size.height, "banner长宽");
            //     this.m_Bannerad.style.top = size.height;//screenHeight - size.height;
            //     this.m_Bannerad.style.left = size.width;//(screenWidth - size.width) / 2;
            //     setBannerPosition && setBannerPosition({ x: this.m_Bannerad.style.left, y: this.m_Bannerad.style.top },
            //         { width: size.width, height: size.height });
        });

        this.m_Bannerad.onLoad(() => {
            if (node == null || !node.active || !node.isValid) {
                console.log("---------------------------------------------------------");
                return
            }
            this.mowBanner++;
            if (this.mowBanner == this.m_BannerIdList.length) {
                this.mowBanner = 0;
            }
            this.bannerSuccese = false;
            this.m_Bannerad
                .show()
                .then(() => {
                    console.log("---------------------Banner广告显示成功");
                    this.m_Bannerad.onHide(() => {
                        console.log('oppo-banner 广告隐藏');
                        onCloseCallBack && onCloseCallBack();
                    });
                })
                .catch((err) => {
                    console.log("---------------------Banner广告组件显示出现问题", JSON.stringify(err));
                });
        });

        this.m_Bannerad.onError((e) => {
            this.bannerSuccese = false
            console.log("---------------------Banner广告加载错误,错误原因：", JSON.stringify(e))
        });

    }

    hideBanner()//隐鲹banner广告
    {
        if (null != this.m_Bannerad) {
            this.m_Bannerad.hide();
            console.log('banner广告隐藏完成');
        }

    }

    /** OPPO九宫格互推盒子 */
    loadAndShowPortalAd(successCallback: () => void, errorCallback: (error: any) => void, completeCallback?: () => void) { // oppo互推盒子 九宫格广告

        if (cc.sys.platform != cc.sys.OPPO_GAME) return;
        if (oppo.getSystemInfoSync().platformVersionCode >= 1076) {
        } else {
            console.log('快应用平台版本号低于1076，暂不支持互推盒子相关 API');
            return;
        }
        // if(this.m_portalAd)
        // {
        //     this.m_portalAd.destroy();
        //     this.m_portalAd=null;
        // }
        if (this.m_portalAd == null) {
            this.m_portalAd = oppo.createGamePortalAd({adUnitId: this.m_PortalId});
        } else {
            this.m_portalAd.show().then(() => {

                successCallback();
                completeCallback && completeCallback();
                console.log('互推盒子九宫格广告显示成功');

            }).catch(() => {

                errorCallback('互推盒子九宫格广告显示失败');
                completeCallback && completeCallback();
                console.log('互推盒子九宫格广告显示失败');
                this.m_portalAd.destroy();
                this.m_portalAd = null;
            });
        }

        this.m_portalAd.load().then(() => {

            console.log('互推盒子九宫格广告加载成功');
            this.m_portalAd.show().then(() => {

                successCallback();
                completeCallback && completeCallback();
                console.log('互推盒子九宫格广告显示成功');

            }).catch((err) => {

                O1HD.getModules().uiControlModule.showMessageBox("互推盒子显示失败~请稍后重试")
                errorCallback('互推盒子九宫格广告显示失败');
                completeCallback && completeCallback();
                console.log('互推盒子九宫格广告显示失败', JSON.stringify(err));
                this.m_portalAd.destroy();
                this.m_portalAd = null;
            });

        }).catch((err) => {

            switch (err.errCode) {
                case 1001:
                    O1HD.getModules().uiControlModule.showMessageBox("拉取次数过多暂时无法显示");
                    break;
                default:
                    O1HD.getModules().uiControlModule.showMessageBox("更多好玩加载失败~请稍后重试~");
                    break
            }
            completeCallback && completeCallback();
            console.log('互推盒子九宫格广告加载失败,error', JSON.stringify(err));
            this.m_portalAd.destroy();
            this.m_portalAd = null;
        });
        // this.m_nativeAd.onError(function(err) {
        //     console.log(JSON.stringify(err),"互推盒子加载失败")
        //   })
        // this.m_portalAd.onClose(function()
        // {
        //     this.m_portalAd.offClose();
        //     this.m_portalAd.destroy();
        //     this.m_portalAd=null;
        // });

    }

    loadAndShowVideoAd(successCallback: () => void,
                       errorCallback: (error: any) => void,
                       completeCallback?: () => void,
                       reportAnalyticsExplain?: string,
                       videoIdPos: number = 0): void {// 显示广告
        console.log('ad show');

        if (this.m_videoAd == null) {
            this.m_videoAd = oppo.createRewardedVideoAd({adUnitId: this.m_adUnitId});
        }

        // O1HD.getManagers().soundMgr.setSoundEnabled(false);
        // if (this.m_videoAd == null) {
        //     console.error('头条广告视频组件不存在');
        //     O1HD.getManagers().soundMgr.setSoundEnabled(true);
        //     errorCallback(null);
        //     return;
        // }
        let onError = (err) => {
            console.log('\
        视频Error');

        }
        this.m_videoAd.offError(onError);
        this.m_videoAd.onError(onError);
        //
        let onClose = (res) => {
            console.log('2\
                视频退出',);

            if (res && res.isEnded) {
                // 给予奖励
                successCallback();
                // O1HD.getManagers().soundMgr.setSoundEnabled(true);
            } else {
                //
                O1HD.getModules().uiControlModule.showMessageBox("完整观看视频才能获得奖励哦~");
                errorCallback('视频中途退出');
                // O1HD.getManagers().soundMgr.setSoundEnabled(true);
            }
            this.m_videoAd.offClose(onClose);
            completeCallback && completeCallback();
            O1HD.getManagers().soundMgr.setSoundEnabled(true);
        };
        //
        this.m_videoAd.load().then(() => {

            this.m_videoAd.onClose(onClose);

            this.m_videoAd.show().then(() => {

            }).catch(() => {
                console.log('1\
            视频显示错误');

                if (this.m_videoAd) {
                    this.m_videoAd.destroy();
                    this.m_videoAd = null;
                }

                O1HD.getModules().uiControlModule.showMessageBox("视频显示错误~请稍后重试");
                errorCallback('视频显示错误');
                this.m_videoAd.offClose(onClose);
                completeCallback && completeCallback();
                O1HD.getManagers().soundMgr.setSoundEnabled(true);

            })

        }).catch(() => {
            console.log('3\
        视频加载错误');
            O1HD.getModules().uiControlModule.showMessageBox("视频加载失败~请稍后重试");
            errorCallback('视频加载错误');
            this.m_videoAd.offClose(onClose);
            completeCallback && completeCallback();
            O1HD.getManagers().soundMgr.setSoundEnabled(true);
            if (this.m_videoAd) {
                this.m_videoAd.destroy();
                this.m_videoAd = null;
            }

        });
        this.m_videoAd.onError((err) => {
            if (this.m_videoAd) {
                this.m_videoAd.destroy();
                this.m_videoAd = null;
            }
            console.log(JSON.stringify(err), "视频加载错误原因")
        })


        //
        // const adUnitId = this.m_adUnitId;
        // console.log('oppo 视频广告', adUnitId);
        // //
        // const videoAd = oppo.createRewardedVideoAd({
        //     adUnitId: adUnitId,
        // });

        // let onError = (e: any) => {
        //     console.error('oppo 视频广告错误', e);
        //     videoAd.offError(onError);
        //     switch (Number(e.errCode)) {
        //         //没有合适的广告 可能该用户无合适的广告
        //         case 1004: {
        //             this.onShareAppMessageGetReward(successCallback, errorCallback, completeCallback, true);
        //             break;
        //         }
        //         //广告发送的次数已达当日上限
        //         // case 120002: {
        //         //     if (videoIdPos + 1 >= this.m_adUnitIdList.length) {
        //         //         this.onShareAppMessageGetReward(successCallback, errorCallback, completeCallback, true);
        //         //     } else {
        //         //         this.loadAndShowVideoAd(successCallback, errorCallback, completeCallback, videoIdPos + 1);
        //         //         return;
        //         //     }
        //         //     break;
        //         // }
        //         default: {
        //             this.showToast({ title: 'err' + e.errCode });
        //             break;
        //         }
        //     }
        // };
        // videoAd.offError(onError);
        // videoAd.onError(onError);

        // //
        // this.showLoading({ title: '请求中' });

        // let onClose = (res) => {
        //     console.log('oppo 视频广告关闭', res);
        //     if (res && res.isEnded) {
        //         // 给予奖励
        //         successCallback();
        //     } else {
        //         //
        //         errorCallback('视频中途退出');
        //     }
        //     completeCallback && completeCallback();
        //     //
        //     videoAd.offClose(onClose);
        // };


        // videoAd.load().then(() => {

        //     videoAd.show().then(() => {
        //         videoAd.onClose(onClose);
        //         this.hideLoading();
        //     }).catch(() => {
        //         errorCallback('视频显示错误');
        //         this.hideLoading();
        //         //
        //         completeCallback && completeCallback();
        //     })


        // }).catch(() => {
        //     errorCallback('视频加载错误');
        //     this.hideLoading();
        //     //
        //     completeCallback && completeCallback();
        // });


        //new
        // console.log("oppo 广告");
        // if (!this.m_videoAd) {
        //     console.warn("oppo 广告创建");
        //     this.m_videoAd = oppo.createRewardedVideoAd({
        //         adUnitId: this.m_adUnitId,
        //     });
        // }
        // const videoAd = oppo.createRewardedVideoAd({
        //     adUnitId: this.m_adUnitId,
        // });
        //
        // let onError = (e: any) => {
        //     switch (Number(e.errCode)) {
        //         case 1004: {
        //             this.onShareAppMessageGetReward(successCallback, errorCallback, completeCallback, true);
        //             break;
        //         }
        //         default: {
        //             this.showToast({title: 'err' + e.errCode});
        //             break;
        //         }
        //     }
        // };
        // videoAd.offError(onError);
        // videoAd.onError(onError);
        //
        // let onshow = () => {
        //     this.hideLoading();
        //     console.log("oppo 广告显示成功");
        //     //
        //     let listener = (res) => {
        //         console.log("oppo 关闭广告", res.isEnded);
        //         if (res.isEnded) {
        //             successCallback();
        //         } else {
        //             console.log("oppo 广告未播放完");
        //             errorCallback('广告未播放完');
        //         }
        //         completeCallback && completeCallback();
        //         //不需要的时候
        //         videoAd.offClose(listener);
        //     }
        //
        //     videoAd.onClose(listener); //这里监听广告的close 事件
        // };
        //
        // let onLoad = () => {
        //     console.log("oppo 广告加载完成");
        //     videoAd
        //         .show()
        //         .then(onshow)
        //         .catch();
        //     // console.log('55');
        // }
        //
        // let tryAgain = (err) => {
        //     console.log("广告组件出现问题 重试一次", err);
        //     // 可以手动加载一次
        //     videoAd.load().then(() => {
        //         console.log("手动加载成功");
        //         // 加载成功后需要再显示广告
        //         videoAd.show().then(onshow).catch((err) => {
        //             this.hideLoading();
        //             console.log("oppo 广告加载???", err);
        //             errorCallback('广告加载异常');
        //             completeCallback && completeCallback();
        //         });
        //     });
        // }
        //
        // this.showLoading({title: '请求中'});
        // videoAd.load().then(onLoad).catch((err) => {
        //     tryAgain(err);
        // });
    }

    login(params: loginParams): void {
        oppo.login(params);
    }

    authorize(params: authorizeParams): void {
        params.success && params.success();
    }


    private m_gameRecordTimeNow: number = 0;

    gameRecordStart(params: gameRecorderStartParams, onStart?: (res) => void): void {
        // if (!this.isHaveShareVideo()) return;
        // this.m_gameRecordTimeNow = Date.now();
        // this.m_gameRecordVideoUrl = null;
        // this.m_gameRecorderManager.onStart((res) => {
        //     console.log('oppo 录屏开始', res);
        //     onStart && onStart(res);
        // });
        // this.m_gameRecorderManager.onStop((res: any) => {
        //     console.log('oppo 录屏结束', res.videoPath);
        //     this.m_gameRecordVideoUrl = res.videoPath;
        // });
        // this.m_gameRecorderManager.start(params);
    }

    gameRecordStop(): boolean {
        return true;
        // if (!this.isHaveShareVideo()) return;
        // if (Date.now() - this.m_gameRecordTimeNow < 3500) {
        //     return;
        // }
        // this.m_gameRecorderManager.stop();
    }

    isGameRecordOver(): boolean {
        return this.m_gameRecordVideoUrl != null;
    }

    shareGameRecordVideo(successCb?: () => void, failCb?: (e?) => void): void {
        // if (!this.isHaveShareVideo()) return;
        // if (this.m_gameRecordVideoUrl) {
        //     oppo.shareAppMessage({
        //         channel: "video",
        //         title: gameConfig.GAME_NAME,
        //         desc: gameConfig.GAME_VIDEO_SHARE_DESC,
        //         extra: {
        //             hashtag_list: [gameConfig.GAME_NAME],
        //             videoPath: this.m_gameRecordVideoUrl, // 可替换成录屏得到的视频地址
        //             videoTopics: [gameConfig.GAME_NAME],
        //             video_title: gameConfig.GAME_VIDEO_SHARE_TITLE
        //         },
        //         success() {
        //             console.log("oppo 分享视频成功");
        //             successCb && successCb();
        //         },
        //         fail(e) {
        //             console.log("oppo 分享视频失败", e);
        //             failCb && failCb(e)
        //         }
        //     });
        // } else {
        //     failCb && failCb(null);
        //     console.log("oppo 分享视频为空");
        // }
    }

    /**production    线上版
     development    测试版
     preview    预览版
     * */
    isProduction(): boolean {
        return OppoApi.isProduction();
    }

    isHaveShareVideo(): boolean {
        return true;
    }

    //判断是否合法APP-ID
    validAppId(): void {
        // oppo.onShow((res: { query, refererInfo: { appId: string, extraData: {} } }) => {
        //     console.log(res.refererInfo.appId,this.m_appId,"appid对比")
        //     if (res.refererInfo.appId != this.m_appId) {
        //         cc.game.end();
        //         throw '非法盗用此程序将受到法律制裁';
        //     } else {
        //         console.log('oppo APP-ID 授权成功');
        //     }
        // });
    }

    reportAnalytics(EventKey: string, data: any): void {
        console.log("oppo 发送埋点", EventKey, JSON.stringify(data))
        // oppo.reportAnalytics(EventKey, data);
        // console.log('oppo 发送埋点', analytics.getKey(), analytics.getObj());
        // oppo.reportAnalytics(analytics.getKey(), analytics.getObj());
        // oppo.reportAnalytics('lookVideo', {
        //     src: 'set_value',
        // });
    }

    clearMemory(): void {

    }

    shareAppMessage(args: any): void {
    }

    protected createRewardedVideoAd(param: TtRewardedVideoAdParam) {
        // this.m_videoAd = oppo.createRewardedVideoAd(param);
    }

    /**production    线上版
     development    测试版
     preview    预览版
     * */
    static isProduction() {
        return true;
    }

    private m_localInBlockCity: boolean = false;

    private CheckLocalToBlockCityList(): void {
        O1HD.getModules().gameHttpModule.getLocalPropertyInfo((res) => {
            this.m_localInBlockCity = gameConfig.IP_BLOCK_CITY.includes(res.city);
        }, (err) => {
            console.debug("-------获取区域信息错误", err);
        });
    }

    LocalInBlockCityList(): boolean {
        return this.m_localInBlockCity;
    }

    showModal(params: ModalParams): void {
    }
}


















